package com.cookor.poi.modules.mx.service;

import com.cookor.poi.common.persistence.DataEntity;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.service.CrudService;
import com.cookor.poi.common.utils.IdGen;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.mx.dao.MxSfjDao;
import com.cookor.poi.modules.mx.entity.MxSfj;
import com.cookor.poi.modules.mx.util.MxUtil;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class MxSfjService extends CrudService<MxSfjDao, MxSfj> {
    public MxSfj get(String id) {
        return (MxSfj) super.get(id);
    }

    public List<MxSfj> findList(MxSfj mxSfj) {
        return super.findList(mxSfj);
    }

    public Page<MxSfj> findPage(Page<MxSfj> page, MxSfj mxSfj) {
        return super.findPage(page, mxSfj);
    }

    @Transactional(readOnly = false)
    public void save(MxSfj mxSfj) {
        super.save(mxSfj);
    }

    @Transactional(readOnly = false)
    public void delete(MxSfj mxSfj) {
        super.delete(mxSfj);
    }

    @Transactional(readOnly = false, rollbackFor = {Exception.class})
    public void sfjSave(String code, String userName, String userType, String mobile, String tenantId, String tenantName, String orderDateChu, String orderDateZhong, String orderDateMo, String equityCode) throws Exception {
        MxSfj mxSfj = ((MxSfjDao) this.dao).getByOrderNo(code, equityCode);
        if (mxSfj == null) {
            mxSfj = new MxSfj();
            mxSfj.setId(IdGen.uuid());
            mxSfj.setOrderNo(code);
            mxSfj.setIsNewRecord(true);
            mxSfj.setUserName(userName);
            mxSfj.setUserType(userType);
            mxSfj.setMobile(mobile);
            mxSfj.setTenantId(tenantId);
            mxSfj.setTenantName(tenantName);
            mxSfj.setEquityCode(equityCode);
            mxSfj.setStatus("0");
            if (StringUtils.isNotBlank(orderDateChu))
                mxSfj.setOrderDateChu((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDateChu));
            if (StringUtils.isNotBlank(orderDateZhong))
                mxSfj.setOrderDateZhong((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDateZhong));
            if (StringUtils.isNotBlank(orderDateMo))
                mxSfj.setOrderDateMo((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDateMo));
            save(mxSfj);
            MxUtil.sendNoticestatus(MxUtil.prodUrl, MxUtil.prodToken, MxUtil.prodId, mxSfj.getOrderNo(), mxSfj
                    .getId(), mxSfj.getEquityCode(), (new SimpleDateFormat("yyyyMMddHHmmss")).format(mxSfj.getUpdateDate()));
        } else {
            if (!mxSfj.getStatus().equals("0"))
                throw new Exception("已预约成功，不能修改");
            mxSfj.setUserName(userName);
            mxSfj.setUserType(userType);
            mxSfj.setEquityCode(equityCode);
            mxSfj.setMobile(mobile);
            mxSfj.setTenantId(tenantId);
            mxSfj.setTenantName(tenantName);
            if (StringUtils.isNotBlank(orderDateChu)) {
                mxSfj.setOrderDateChu((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDateChu));
            } else {
                mxSfj.setOrderDateChu(null);
            }
            if (StringUtils.isNotBlank(orderDateZhong)) {
                mxSfj.setOrderDateZhong((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDateZhong));
            } else {
                mxSfj.setOrderDateZhong(null);
            }
            if (StringUtils.isNotBlank(orderDateMo)) {
                mxSfj.setOrderDateMo((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDateMo));
            } else {
                mxSfj.setOrderDateMo(null);
            }
            save(mxSfj);
        }
    }

    @Transactional(readOnly = false, rollbackFor = {Exception.class})
    public void gfajSave(String code, String userName, String userType, String mobile, String tenantId, String tenantName, String orderDate, String equityCode) throws Exception {
        MxSfj mxSfj = ((MxSfjDao) this.dao).getByOrderNo(code, equityCode);
        if (mxSfj == null) {
            mxSfj = new MxSfj();
            mxSfj.setId(IdGen.uuid());
            mxSfj.setOrderNo(code);
            mxSfj.setIsNewRecord(true);
            mxSfj.setUserName(userName);
            mxSfj.setUserType(userType);
            mxSfj.setMobile(mobile);
            mxSfj.setTenantId(tenantId);
            mxSfj.setTenantName(tenantName);
            mxSfj.setStatus("0");
            mxSfj.setEquityCode(equityCode);
            mxSfj.setOrderDate((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDate));
            save(mxSfj);
            MxUtil.sendNoticestatus(MxUtil.prodUrl, MxUtil.prodToken, MxUtil.prodId, mxSfj.getOrderNo(), mxSfj
                    .getId(), mxSfj.getEquityCode(), (new SimpleDateFormat("yyyyMMddHHmmss")).format(mxSfj.getUpdateDate()));
        } else {
            if (!mxSfj.getStatus().equals("0"))
                throw new Exception("已经预约成功了，修改失败");
            mxSfj.setTenantId(tenantId);
            mxSfj.setUserName(userName);
            mxSfj.setUserType(userType);
            mxSfj.setMobile(mobile);
            mxSfj.setTenantId(tenantId);
            mxSfj.setTenantName(tenantName);
            mxSfj.setEquityCode(equityCode);
            mxSfj.setOrderDate((new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(orderDate));
            save(mxSfj);
        }

    }

    @Transactional(readOnly = false, rollbackFor = {Exception.class})
    public void fzSaveTest(String code, String userName, String userType, String mobile, String equityCode, String tongue, String coating) throws Exception {
        Calendar calendar = Calendar.getInstance();
        // 将时、分、秒、毫秒字段设置为0
//        calendar.set(Calendar.HOUR_OF_DAY, 0);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
        Date today = calendar.getTime();
        MxSfj mxSfj = this.dao.getByOrderNoDate(code, equityCode, today);
//        if (mxSfj == null) {
            mxSfj = new MxSfj();
            mxSfj.setId(IdGen.uuid());
            mxSfj.setOrderNo(code);
            mxSfj.setIsNewRecord(true);
            mxSfj.setUserName(userName);
            mxSfj.setUserType(userType);
            mxSfj.setMobile(mobile);
            mxSfj.setTenantId("");
            mxSfj.setTenantName("");
            mxSfj.setStatus("0");
            mxSfj.setEquityCode(equityCode);
            mxSfj.setOrderDate(today);
            mxSfj.setTenantId(tongue);
            mxSfj.setTenantName(coating);
            save(mxSfj);
            MxUtil.sendNoticestatus(MxUtil.testUrl, MxUtil.testToken, MxUtil.testId, mxSfj.getOrderNo(), mxSfj
                    .getId(), mxSfj.getEquityCode(), (new SimpleDateFormat("yyyyMMddHHmmss")).format(mxSfj.getUpdateDate()));
//        }else{
//            if(mxSfj.getStatus().equals("1")){
//                throw new Exception("今天已经检测过了");
//            }
//        }
    }

    @Transactional(readOnly = false, rollbackFor = {Exception.class})
    public void fzSave(String code, String userName, String userType, String mobile, String equityCode, String tongue, String coating) throws Exception {
        Calendar calendar = Calendar.getInstance();
        // 将时、分、秒、毫秒字段设置为0
//        calendar.set(Calendar.HOUR_OF_DAY, 0);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
        Date today = calendar.getTime();
        MxSfj mxSfj = this.dao.getByOrderNoDate(code, equityCode, today);
//        if (mxSfj == null) {
            mxSfj = new MxSfj();
            mxSfj.setId(IdGen.uuid());
            mxSfj.setOrderNo(code);
            mxSfj.setIsNewRecord(true);
            mxSfj.setUserName(userName);
            mxSfj.setUserType(userType);
            mxSfj.setMobile(mobile);
            mxSfj.setTenantId("");
            mxSfj.setTenantName("");
            mxSfj.setStatus("0");
            mxSfj.setEquityCode(equityCode);
            mxSfj.setOrderDate(today);
            mxSfj.setTenantId(tongue);
            mxSfj.setTenantName(coating);
            save(mxSfj);
            MxUtil.sendNoticestatus(MxUtil.prodUrl, MxUtil.prodToken, MxUtil.prodId, mxSfj.getOrderNo(), mxSfj
                    .getId(), mxSfj.getEquityCode(), (new SimpleDateFormat("yyyyMMddHHmmss")).format(mxSfj.getUpdateDate()));
//        }else{
//            if(mxSfj.getStatus().equals("1")){
//                throw new Exception("今天已经检测过了");
//            }
//        }
    }
}
